$(function() {
//  var e = getCookie("key");
//  if (e) {
//      pageChange("../member/member.html");
//      return
//  }
    $.sValid.init({
    	rules:{
    		username: {
    			required:true
    		},
    		password: {
    			required:true,
                minlength:6,
                maxlength:12
    		},
    		password_repeat:{
				required:true,
                equalTo: "#password"
    		},
    		true_name: {
    			required:true
    		},
    		unitname: {
    			required:true
    		}
		}, 
		messages: {
			username: {
				required: "账号必须填写",
			},
			password: {
				 required: "密码必填",
		         minlength: "密码长度不能小于 6 个字符",
		         maxlength: "密码长度不能大于 18个字符",
			},
			password_repeat: {
				 required: "确认密码必填",
				 equalTo: "两次密码输入不一致",
			},
			true_name: {
				required: "姓名必填!",
			},
			unitname:  {
				required: "所属企业必填!",
			},
	   },
       	callback: function(e, r, a) {
            if (e.length > 0) {
                var i = "";
                $.map(r,function(e, r) {i += "<p>" + e + "</p>"});
                layer.open({content: i, skin: 'msg', time: 2});
            }
        }
	});
    $("#register-btn").on('tap',function() {
        var username = $("input[name=username]").val();
        var password = $("input[name=password]").val();
        var passeword_reapt = $("input[name=password_repeat]").val();
        var true_name = $("input[name=true_name]").val();
        var unitname = $("input[name=unitname]").val();
        var phone = $("input[name=phone]").val();
//      var wechat = $("input[name=wechat]").val();
        var note = $("input[name=note]").val();  
        if ($.sValid()) {
            $.ajax({
            	type: "post", 
            	url: ApiUrl, 
        	    contentType: 'application/json',
                data:JSON.stringify({
	        	    "op":"Create",
	                "LoginCode": username,
	                "PassWord": password,
	                "LoginName": true_name,
	                "UnitName":unitname,
	                "LinkTel": phone,
//	                "WXID":wechat,
	                "Note":note
                }), 
                success: function(e) {
                	e = JSON.parse(e);
                	console.log(e);
	                if (e.status == 0) {
			           layer.open({content: e.Message, skin: 'msg', time: 2});
			           return;
                    } else {
        		        layer.open({
					        shadeClose: false,
					        content: '注册'+ e.Message+'，等待审核'
					        , btn: ['确定']
					        , yes: function (index) {
//					        	pageChange("../member/login_mobile.html");
								WeixinJSBridge.invoke('closeWindow',{},function(res){
								//alert(res.err_msg);
								});
					        }
				        });
                    }
                }
        })
        }
    })
    
    $(document).on('tap','.data-list ul li',function(){
    	$('#unitname').val($(this).data('content'));
    	$('.data-list').hide();
    })
});
function writeAll(e) {
    if (e.val().length > 3) {
	    $.ajax({
        	type: "post", 
        	url: ApiUrl, 
    	    contentType: 'application/json',
            data:JSON.stringify({
        	    "op":"Unit",
        	    "keyword":e.val()
            }), 
            success: function(e) {
            	console.log(e);
                e = JSON.parse(e);
                console.log(e.Main);
                if(e.Main.length > 0){
                	var i = 0;
                $('.data-list').show();
                e.Main.forEach(function(item) {
                	i++;
            	    var html = '<li data-content="'+item.UnitName+'">';
            	    html += item.UnitName;
            	    html += '</li>';
            	    $('.data-list ul').html(html);
                })
                }else{
                	$('.data-list').hide();
                };
                
            }
        })
    }else{
    	$('.data-list').hide();
    }
       
}
